package face.pay.web.cpn.activityInfo;

import face.pay.cpn.activityInfo.ICpnActivityInfoService;
import face.pay.entity.CpnActivityInfo;
import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.security.exception.BaseException;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Classname CpnActivityInfoController
 * @Description JJL
 * @Date 2019/12/2 9:42
 */
@Controller
@RequestMapping("/activity")
public class CpnActivityInfoController {

    @Autowired
    private ICpnActivityInfoService cpnActivityInfoService;

    @RequestMapping("/index")
    public String index() {
        return "cpn/cpnActivityInfo";
    }

    @RequestMapping("/queryData")
    @ResponseBody
    public PageData data(PageParam pageParam, CpnActivityInfo activityInfo, @CurrentUser UserInfo userInfo) throws BaseException {

        activityInfo.setUserId(userInfo.getId());
        activityInfo.setOemId(userInfo.getOemId());
        return this.cpnActivityInfoService.queryPage(activityInfo, pageParam);

    }

    /**
     * 编辑活动信息
     * @param activityInfo
     * @param userInfo
     * @return
     * @throws BaseException
     */
    @RequestMapping("/edit")
    @ResponseBody
    public ResultMessage editActivityInfo(CpnActivityInfo activityInfo, @CurrentUser UserInfo userInfo) throws BaseException {
        return cpnActivityInfoService.modifyActivityInfo(activityInfo,userInfo);
    }

    /**
     * 封面上传
     *
     * @param file
     * @param loginUser
     * @param conver_text 封面摘要简介。
     * @return
     */
    @RequestMapping("/addConverText")
    @ResponseBody
    public ResultMessage addConverText(@RequestParam("file") MultipartFile file, @CurrentUser UserInfo loginUser,String conver_text,Integer id) throws Exception {
        byte[] fileBytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        return this.cpnActivityInfoService.addConverText(fileBytes, originalFilename, loginUser,conver_text,id);
    }

    /**
     * 开启活动  关闭活动
     *
     * @param loginUser
     * @param cpnActivityInfo
     * @return
     */
    @RequestMapping("/openAndCloseActivity")
    @ResponseBody
    public ResultMessage openAndCloseActivity(@CurrentUser UserInfo loginUser,CpnActivityInfo cpnActivityInfo) throws Exception {
        return this.cpnActivityInfoService.openAndCloseActivity(loginUser,cpnActivityInfo);
    }

    /**
     * 图文上传
     *
     * @param file
     * @param loginUser
     * @param text 图文描述。
     * @return
     */
    @RequestMapping("/addImageUrl")
    @ResponseBody
    public ResultMessage addImageUrl(@RequestParam("file") MultipartFile file, @CurrentUser UserInfo loginUser,String text,Integer id) throws Exception {
        byte[] fileBytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        return this.cpnActivityInfoService.addImageUrl(fileBytes, originalFilename, loginUser,text,id);
    }
}
